package com.mangosteen.interviewji.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mangosteen.interviewji.annotation.AuthCheck;
import com.mangosteen.interviewji.common.BaseResponse;
import com.mangosteen.interviewji.common.DeleteRequest;
import com.mangosteen.interviewji.common.ErrorCode;
import com.mangosteen.interviewji.common.ResultUtils;
import com.mangosteen.interviewji.constant.UserConstant;
import com.mangosteen.interviewji.exception.BusinessException;
import com.mangosteen.interviewji.exception.ThrowUtils;
import com.mangosteen.interviewji.model.dto.questionAssociate.QuestionAssociateAddRequest;
import com.mangosteen.interviewji.model.dto.questionAssociate.QuestionAssociateQueryRequest;
import com.mangosteen.interviewji.model.dto.questionAssociate.QuestionAssociateUpdateRequest;
import com.mangosteen.interviewji.model.entity.QuestionAssociate;
import com.mangosteen.interviewji.model.entity.User;
import com.mangosteen.interviewji.model.vo.QuestionAssociateVO;
import com.mangosteen.interviewji.service.QuestionAssociateService;
import com.mangosteen.interviewji.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 题目关联接口
 *
 */
@RestController
@RequestMapping("/questionAssociate")
@Slf4j
public class QuestionAssociateController {

    @Resource
    private QuestionAssociateService questionAssociateService;

    @Resource
    private UserService userService;

    // region 增删改查

    /**
     * 创建题目关联
     *
     * @param questionAssociateAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addQuestionAssociate(@RequestBody QuestionAssociateAddRequest questionAssociateAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(questionAssociateAddRequest == null, ErrorCode.PARAMS_ERROR);
        // todo 在此处将实体类和 DTO 进行转换
        QuestionAssociate questionAssociate = new QuestionAssociate();
        BeanUtils.copyProperties(questionAssociateAddRequest, questionAssociate);
        // 数据校验
        questionAssociateService.validQuestionAssociate(questionAssociate, true);

        // 写入数据库
        boolean result = questionAssociateService.save(questionAssociate);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        // 返回新写入的数据 id
        long newQuestionAssociateId = questionAssociate.getId();
        return ResultUtils.success(newQuestionAssociateId);
    }




    /**
     * 根据 id 获取题目关联（封装类）
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<QuestionAssociateVO> getQuestionAssociateVOById(long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        QuestionAssociate questionAssociate = questionAssociateService.getById(id);
        ThrowUtils.throwIf(questionAssociate == null, ErrorCode.NOT_FOUND_ERROR);
        // 获取封装类
        return ResultUtils.success(questionAssociateService.getQuestionAssociateVO(questionAssociate, request));
    }

    /**
     * 分页获取题目关联列表（仅管理员可用）
     *
     * @param questionAssociateQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<QuestionAssociate>> listQuestionAssociateByPage(@RequestBody QuestionAssociateQueryRequest questionAssociateQueryRequest) {
        long current = questionAssociateQueryRequest.getCurrent();
        long size = questionAssociateQueryRequest.getPageSize();
        // 查询数据库
        Page<QuestionAssociate> questionAssociatePage = questionAssociateService.page(new Page<>(current, size),
                questionAssociateService.getQueryWrapper(questionAssociateQueryRequest));
        return ResultUtils.success(questionAssociatePage);
    }

    /**
     * 分页获取题目关联列表（封装类）
     *
     * @param questionAssociateQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<QuestionAssociateVO>> listQuestionAssociateVOByPage(@RequestBody QuestionAssociateQueryRequest questionAssociateQueryRequest,
                                                               HttpServletRequest request) {
        long current = questionAssociateQueryRequest.getCurrent();
        long size = questionAssociateQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        Page<QuestionAssociate> questionAssociatePage = questionAssociateService.page(new Page<>(current, size),
                questionAssociateService.getQueryWrapper(questionAssociateQueryRequest));
        // 获取封装类
        return ResultUtils.success(questionAssociateService.getQuestionAssociateVOPage(questionAssociatePage, request));
    }

    /**
     * 分页获取当前登录用户创建的题目关联列表
     *
     * @param questionAssociateQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<QuestionAssociateVO>> listMyQuestionAssociateVOByPage(@RequestBody QuestionAssociateQueryRequest questionAssociateQueryRequest,
                                                                 HttpServletRequest request) {
        ThrowUtils.throwIf(questionAssociateQueryRequest == null, ErrorCode.PARAMS_ERROR);
        // 补充查询条件，只查询当前登录用户的数据
        User loginUser = userService.getLoginUser(request);
        questionAssociateQueryRequest.setUserId(loginUser.getId());
        long current = questionAssociateQueryRequest.getCurrent();
        long size = questionAssociateQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        Page<QuestionAssociate> questionAssociatePage = questionAssociateService.page(new Page<>(current, size),
                questionAssociateService.getQueryWrapper(questionAssociateQueryRequest));
        // 获取封装类
        return ResultUtils.success(questionAssociateService.getQuestionAssociateVOPage(questionAssociatePage, request));
    }

    /**
     * 编辑题目关联（给用户使用）
     *
     * @param questionAssociateEditRequest
     * @param request
     * @return
     */
    @PostMapping("/edit")
    public BaseResponse<Boolean> editQuestionAssociate(@RequestBody QuestionAssociateUpdateRequest questionAssociateEditRequest, HttpServletRequest request) {
        if (questionAssociateEditRequest == null || questionAssociateEditRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // todo 在此处将实体类和 DTO 进行转换
        QuestionAssociate questionAssociate = new QuestionAssociate();
        BeanUtils.copyProperties(questionAssociateEditRequest, questionAssociate);
        // 数据校验
        questionAssociateService.validQuestionAssociate(questionAssociate, false);
        User loginUser = userService.getLoginUser(request);
        // 判断是否存在
        long id = questionAssociateEditRequest.getId();
        QuestionAssociate oldQuestionAssociate = questionAssociateService.getById(id);
        ThrowUtils.throwIf(oldQuestionAssociate == null, ErrorCode.NOT_FOUND_ERROR);
        // 操作数据库
        boolean result = questionAssociateService.updateById(questionAssociate);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    // endregion
}
